<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control;

use yii\base\InvalidArgumentException;

/**
 * 下拉选择(layui select)
 */
class SelectLayui extends Control
{
    /**
     * @var array
     */
    private $_options = [
        'control' => 'select_layui',
        'label' => '',
        'placeholder' => '请选择',
        'default' => '',
        'options' => [],
        'required' => false,
        'comment' => '',
        'help' => '',
        'layout' => 12,
        'style' => '',
        'class' => '',
        'attribute' => '',
        'disabled' => false,
        'search' => false,
        'search_caseSensitive' => 'false',
        'search_fuzzy' => 'false',
        'creatable' => false,
    ];

    /**
     * 当前开启搜索时，是否允许创建新的option。
     * @param bool $bool
     * @return $this
     */
    public function creatable(bool $bool): SelectLayui
    {
        $this->_options['creatable'] = $bool;
        return $this;
    }

    /**
     * 当前开启搜索时，是否开启模糊匹配
     * @param bool $bool
     * @return $this
     */
    public function search_fuzzy(bool $bool): SelectLayui
    {
        $this->_options['search_fuzzy'] = $bool ? 'true' : 'false';
        return $this;
    }

    /**
     * 当前开启搜索时，是否区分大小写
     * @param bool $bool
     * @return $this
     */
    public function search_caseSensitive(bool $bool): SelectLayui
    {
        $this->_options['search_caseSensitive'] = $bool ? 'true' : 'false';
        return $this;
    }

    /**
     * 是否开启搜索
     * @param bool $bool
     * @return $this
     */
    public function search(bool $bool): SelectLayui
    {
        $this->_options['search'] = $bool;
        return $this;
    }

    /**
     * 是否禁用控件
     * @param bool $bool
     * @return $this
     */
    public function disabled(bool $bool): SelectLayui
    {
        $this->_options['disabled'] = $bool;
        return $this;
    }

    /**
     * 控件标签
     * @param string|null $label
     * @return $this
     */
    public function label(?string $label): SelectLayui
    {
        $this->_options['label'] = $label;
        return $this;
    }

    /**
     * 站位文本
     * @param string $placeholder
     * @return $this
     */
    public function placeholder(string $placeholder): SelectLayui
    {
        $this->_options['placeholder'] = $placeholder;
        return $this;
    }

    /**
     * 默认值
     * @param string $default
     * @return $this
     */
    public function defaultValue(string $default): SelectLayui
    {
        $this->_options['default'] = $default;
        return $this;
    }

    /**
     * 选项
     * @param array $options
     * ```php
     * // label: 选项名  value: 选项值  disabled: 是否禁用该选项
     * $this->options([
     *     ['value' => 1, 'label'=> '钻石', 'disabled' => false],
     *     ['value' => 2, 'label'=> '黄金', 'disabled' => false],
     *     ['value' => 3, 'label'=> '白银', 'disabled' => false],
     * ]);
     * ```
     * @return $this
     */
    public function options(array $options): SelectLayui
    {
        $this->_options['options'] = $options;
        return $this;
    }

    /**
     * 是否必填
     * @param bool $required
     * @return $this
     */
    public function required(bool $required = true): SelectLayui
    {
        $this->_options['required'] = $required;
        return $this;
    }

    /**
     * 字段注释
     * @param string $comment
     * @return $this
     */
    public function comment(string $comment): SelectLayui
    {
        $this->_options['comment'] = $comment;
        return $this;
    }

    /**
     * 帮助文字
     * @param string $help
     * @return $this
     */
    public function help(string $help): SelectLayui
    {
        $this->_options['help'] = $help;
        return $this;
    }

    /**
     * 布局，1-12
     * @param int $layout
     * @return $this
     */
    public function layout(int $layout): SelectLayui
    {
        $this->_options['layout'] = $layout;
        return $this;
    }

    /**
     * CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style): SelectLayui
    {
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class): SelectLayui
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array|string $attribute
     * @return $this
     */
    public function attribute($attribute): SelectLayui
    {
        $this->formatHtmlAttribute($attribute);
        $this->_options['attribute'] = $attribute;

        return $this;
    }

    /**
     * @return array
     */
    public function _toArray(): array
    {
        $options = $this->_options;

        $addonAttrs = [];
        if ($options['disabled']) $addonAttrs['disabled'] = '';
        if ($options['search']) {
            $addonAttrs['lay-search'] = "{caseSensitive:{$options['search_caseSensitive']}, fuzzy: {$options['search_fuzzy']}}";
        }
        if ($options['creatable']) $addonAttrs['lay-creatable'] = '';

        if (!empty($addonAttrs)) {
            $this->formatHtmlAttribute($addonAttrs);
        } else {
            $addonAttrs = '';
        }

        $options['attribute'] .= $addonAttrs;

        return $options;
    }
}